package com.yaxin.heshanghui.activity;

import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.framework.Utils.Utils;
import com.yaxin.heshanghui.R;
import com.yaxin.heshanghui.adapter.BiuooCommonAdapter;
import com.yaxin.heshanghui.adapter.BiuooListItem;
import com.yaxin.heshanghui.adapter.BiuooViewHolder;
import com.yaxin.heshanghui.base.BaseOnClickListener;
import com.yaxin.heshanghui.base.BiuooBaseActivity;
import com.yaxin.heshanghui.customview.CustomListView;
import com.yaxin.heshanghui.customview.TitleView;
import com.yaxin.heshanghui.utils.BiuooCollectionUtil;

import java.util.ArrayList;
import java.util.List;


public class BiuooGuideChooseNothingActivity extends BiuooBaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    private TitleView titleView;
    private CustomListView customLv1;
    private CustomListView customLv2;
    private BiuooCommonAdapter<BiuooListItem> adapter1;
    private BiuooCommonAdapter<BiuooListItem> adapter2;
    private List<BiuooListItem> list1 = new ArrayList<>();
    private List<BiuooListItem> list2 = new ArrayList<>();
    private boolean _checked;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setTranslucentStatus(this, true);
        Utils.setStatusBarTintDrawable(this, R.mipmap.biuoo_bg_top);
        setContentView(R.layout.biuoo_activity_guide_buy_nochoose);
        initView();
        initList1();
        initList2();
    }


    private void initList1() {
        BiuooListItem item = new BiuooListItem();
        item.setTitle("外置柔光power");
        item.setPrice("¥300.00");
        item.setSource("货源:华强北");
        item.setCount("库存:" + 1035);
        BiuooListItem item2 = new BiuooListItem();
        item2.setTitle("外置柔光22");
        item2.setPrice("¥102.00");
        item2.setSource("货源:华强北");
        item2.setCount("库存:" + 1059);
        list1.add(item);
        list1.add(item2);
        adapter1 = initAdapter(adapter1, true);
        customLv1.setAdapter(adapter1);
    }

    private void initList2() {
        BiuooListItem item = new BiuooListItem();
        item.setTitle("外置柔光qw");
        item.setPrice("¥110");
        item.setSource("货源:华强北");
        item.setCount("库存:" + 1033);
        BiuooListItem item2 = new BiuooListItem();
        item2.setTitle("外置柔光qwa");
        item2.setPrice("¥110");
        item2.setSource("货源:华强北");
        item2.setCount("库存:" + 1523);
        list2.add(item);
        list2.add(item2);
        adapter2 = initAdapter(adapter2, false);
        customLv2.setAdapter(adapter2);
    }

    private BiuooCommonAdapter<BiuooListItem> initAdapter(BiuooCommonAdapter<BiuooListItem> adapter, boolean left) {
        if (adapter == null) {
            adapter = new BiuooCommonAdapter<BiuooListItem>(BiuooGuideChooseNothingActivity.this, left ? list1 : list2, R.layout.biuoo_listitem_guide) {
                @Override
                public View getListItemview(BiuooViewHolder viewHolder, View view, final BiuooListItem item, int position, ViewGroup parent) {
                    viewHolder.getView(R.id.tv_source).setVisibility(View.VISIBLE);
                    viewHolder.setText(R.id.tv_source, item.getSource());
                    viewHolder.setText(R.id.tv_title, item.getTitle());
                    viewHolder.setText(R.id.tv_price, item.getPrice());
                    viewHolder.setText(R.id.tv_count, item.getCount());
                    CheckBox checkbox = viewHolder.getView(R.id.checkbox);
                    checkbox.setChecked(item.isChecked());
                    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            item.setChecked(isChecked);
//                            notifyDataSetChanged();
                            if (isChecked) {
                                _checked = true;
                                titleView.setRightTxt("下一步");
                                return;
                            } else if (BiuooCollectionUtil.getInstance().isEmpty(list1)) {
                                int len = list1.size();
                                for (int i = 0; i < len; i++) {
                                    if (list1.get(i).isChecked()) {
                                        _checked = true;
                                        titleView.setRightTxt("下一步");
                                        return;
                                    }
                                }
                            } else if (BiuooCollectionUtil.getInstance().isEmpty(list2)) {
                                int len = list2.size();
                                for (int i = 0; i < len; i++) {
                                    if (list2.get(i).isChecked()) {
                                        _checked = true;
                                        titleView.setRightTxt("下一步");
                                        return;
                                    }
                                }
                            }
                            _checked = false;
                            titleView.setRightVisible(false);
                        }
                    });
                    return null;
                }
            };
        }
        return adapter;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                break;
            default:
                break;
        }
    }


    private void initView() {
        titleView = (TitleView) findViewById(R.id.titleView);
        titleView.setTitle("导购").setRightVisible(false).setRightClickListner(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {
                //点击下一步
                startActivity(BiuooSaleActivity.class);
            }
        });
        customLv1 = (CustomListView) findViewById(R.id.customLv1);
        customLv2 = (CustomListView) findViewById(R.id.customLv2);
    }

}
